package com.zhongkai.encryptAES;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class EncryptAES {
	private SecretKey mDstKey = null;
	private Cipher mCipher = null;
	
	public EncryptAES( ) throws NoSuchAlgorithmException, NoSuchPaddingException {
		Security.addProvider( new com.sun.crypto.provider.SunJCE( ) );
		mDstKey = KeyGenerator.getInstance( "AES" ).generateKey( );
		mCipher = Cipher.getInstance( "AES" );
	}
	
	public byte [] Encrypt (byte[] src) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
		mCipher.init( Cipher.ENCRYPT_MODE, mDstKey );
		return mCipher.doFinal( src );
	}
	
	public byte [] Decrypt(byte [] dst) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
		mCipher.init( Cipher.DECRYPT_MODE, mDstKey );
		return mCipher.doFinal( dst );
	}
}
